from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSql import *


class TodoTable(QWidget):
    
    edit_item = Signal(int)
    COLUMN_ID = 0
    COLUMN_DATE = 1
    COLUMN_TASK = 2
    COLUMN_CATEGORY = 3
    
    def __init__(self, table):
        super(TodoTable, self).__init__()
        self.table = table
        
    def setup(self):
        model = QSqlTableModel()
        model.setTable("tasks")

        model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        model.select()
        
        model.setHeaderData(self.COLUMN_ID, Qt.Horizontal, "ID")
        model.setHeaderData(self.COLUMN_DATE, Qt.Horizontal, "Date")
        model.setHeaderData(self.COLUMN_TASK, Qt.Horizontal, "Task")
        model.setHeaderData(self.COLUMN_CATEGORY, Qt.Horizontal, "Category")
        
        self.table.setModel(model)
        self.table.hideColumn(self.COLUMN_ID)
        self.table.hideColumn(self.COLUMN_CATEGORY)
        self.table.setSortingEnabled(True)
        self.table.activated.connect(self.row_activated)
        
    def row_activated(self, cell):
        row = cell.row()
        id = self.table.model().data(self.table.model().index(row, self.COLUMN_ID))
        self.edit_item.emit(id)